package com.ysstech.common.util.function;

import java.util.Map;

import com.googlecode.aviator.exception.ExpressionRuntimeException;
import com.googlecode.aviator.runtime.function.AbstractVariadicFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

@Slf4j
public class FuncionAuxilary extends AbstractVariadicFunction {
    /**
     */
    @Override
    public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) throws ExpressionRuntimeException {
        try {
            StringBuffer buffer = new StringBuffer();
            if (CollectionUtils.isEmpty(env)) {
                return null;
            }
            for (AviatorObject each : args) {
                if (each.getValue(env) == null) {
                    continue;
                }
                String value = FunctionUtils.getStringValue(each, env);
                if (StringUtils.isBlank(value)) {
                    continue;
                }
                buffer.append(value).append("-");
            }
            if (buffer.length() >= 1) {
                buffer.deleteCharAt(buffer.length() - 1);
            }
            return new AviatorString(buffer.toString());
        } catch (Exception ex) {
            log.error("执行科目辅助项拼接公式出错" + ex.toString());
        }
        return null;
    }

    /**
     * 返回方法名
     */
    @Override
    public String getName() {
        return "AUXILARY";
    }


}
